package com.webcamtracker.tracker.example.browser;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;


public class BrowserManager {

    private List<File> files;
    private int index = 0;

    public boolean hasNext() {
        return files.size() > index;
    }

    public File next() {
        File result = files.get(index);
        index++;
        return result;
    }

    public File back() {
        index--;
        return files.get(index);
    }

    public boolean hasBack() {
        return index > 0;
    }

    public void load(File dir) {
        files = new ArrayList<File>();
        loadFilesImpl(dir, files);
    }

    private void loadFilesImpl(File dir, List<File> files) {
        File[] files1 = dir.listFiles(new ImageFilter());
        for (File file : files1) {
            if (file.isDirectory()) {
                loadFilesImpl(file, files);
            } else {
                files.add(file);
            }
        }
    }

    private static class ImageFilter implements FileFilter {

        public boolean accept(File pathname) {
            return pathname.isDirectory() || pathname.getName().endsWith(".jpg");
        }
    }
}
